<?php
namespace app\admin\business;

class Admin extends Base {

    public function login($data) :bool {
        try{
            $model = (new \app\model\mysql\Admin())->getInfoByUsername($data["username"]);
            if (empty($model) || $model->status != config("status.mysql.table_normal")){
                throw new \Exception("不存在该用户");
            }
            // 判断密码是否正确
            if ($model->password != md5($data["password"])) {
                throw new \Exception("用户密码不正确");
            }

            // 记录信息到mysql表里
            $result = $model->updateLoginInfo($model->id, [
                "last_login_time" => time(),
                "last_login_ip" => request()->ip(),
                "updated_time" => time()
            ]);

            if (!$result) {
                throw new \Exception("登录失败");
            }

            // 记录session
            session(config("admin.session_user"), $model->toArray());
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }
}